package com.dmooo.ymx.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.dmooo.ymx.CaiNiaoApplication;
import com.dmooo.ymx.MainActivity;
import com.dmooo.ymx.R;
import com.dmooo.ymx.bean.BannerBean;
import com.dmooo.ymx.bean.PddClient;
import com.dmooo.ymx.bean.Response;
import com.dmooo.ymx.common.SPUtils;
import com.dmooo.ymx.config.Constants;
import com.dmooo.ymx.https.HttpUtils;
import com.dmooo.ymx.https.onOKJsonHttpResponseHandler;
import com.dmooo.ymx.utils.CornerTransform;
import com.dmooo.ymx.widget.LoadingDialog;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;


public class SplashActivity extends Activity {

    ImageView service_icon1;
    /**
     * @param msg 加载框提示语
     *            显示圆形加载进度对话框
     */
    private LoadingDialog loadingDialog;
    protected void showLoadingDialog(String msg) {
        if (!this.isDestroyed() || !this.isFinishing()) {
            if (loadingDialog == null) {
                loadingDialog = LoadingDialog.createDialog(this);
                loadingDialog.setMessage(msg);
                loadingDialog.setCanceledOnTouchOutside(false);
            }
            loadingDialog.show();
        }
    }

    /**
     * 关闭进度对话框
     */
    protected void closeLoadingDialog() {
        if (!this.isDestroyed() || !this.isFinishing()) {
            if (loadingDialog != null && loadingDialog.isShowing()) {
                loadingDialog.dismiss();
                loadingDialog = null;
            }
        }
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_first);
        getBannerMsg();
        service_icon1=findViewById(R.id.service_icon1);
    }

    private List<BannerBean> images = new ArrayList<>();
    private void getBannerMsg() {
        RequestParams params = new RequestParams();
        params.put("id",121);
        HttpUtils.post(Constants.GET_BANNER_DETAIL, params, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                // 输出错误信息
            }

            @Override
            public void onFinish() {
                // 隐藏进度条
                super.onFinish();
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                try {
                    JSONObject jsonObject = new JSONObject(responseString);
                    //返回码
                    int code = jsonObject.optInt("code");
                    //返回码说明
                    String msg = jsonObject.optString("msg");
                    if (0 == code) {
                        Glide.with(SplashActivity.this).load(Constants.APP_IP+jsonObject.getJSONObject("data").getJSONObject("bannerMsg").getString("img")).into(service_icon1);
                    } else {
//                        showToast(msg);
                    }
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            RequestParams requestParams = new RequestParams();
                            HttpUtils.post(Constants.APP_IP+"/app.php?c=Index&a=getAppSet", requestParams, new TextHttpResponseHandler() {
                                @Override
                                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//                    showToast(throwable.getMessage());
//                    finish();
                                }

                                @Override
                                public void onStart() {
                                    super.onStart();
                                }

                                @Override
                                public void onFinish() {
                                    super.onFinish();
                                }

                                @Override
                                public void onSuccess(int statusCode, Header[] headers, String responseString) {
                                    try {
                                        JSONObject object = new JSONObject(responseString);
                                        Log.d("dsfads",responseString);
                                        if (object.getInt("code") == 0) {
                                            object=object.getJSONObject("data");
                                            SPUtils.saveStringData(SplashActivity.this,"wx_appid",object.getString("wxpay_appid"));
                                            SPUtils.saveStringData(SplashActivity.this,"wx_secret",object.getString("wxpay_appsecret"));
                                            SPUtils.saveStringData(SplashActivity.this,"tbk_appkey",object.getString("tbk_appkey"));
                                            SPUtils.saveStringData(SplashActivity.this,"tbk_relation_code",object.getString("tbk_relation_code"));
                                            SPUtils.saveStringData(SplashActivity.this,"tbk_appsecret",object.getString("tbk_appsecret"));
                                            SPUtils.saveStringData(SplashActivity.this,"pdd_client_id",object.getString("pdd_client_id"));
                                            SPUtils.saveStringData(SplashActivity.this,"pdd_client_secret",object.getString("pdd_client_secret"));
                                            SPUtils.saveStringData(CaiNiaoApplication.getAppContext(), "jd_key", object.getString("jd_android_key"));
                                            SPUtils.saveStringData(CaiNiaoApplication.getAppContext(), "jd_secret", object.getString("jd_android_secret"));
                                            SPUtils.saveStringData(CaiNiaoApplication.getAppContext(), "jd_pos_id", object.getString("jd_position_id"));
                                            SPUtils.saveStringData(CaiNiaoApplication.getAppContext(), "JD_APP_KEY_NEW", object.getString("jd_appkey_myxq"));
                                            Constants.setInfo();
                                            PddClient.setInfo();
                                            init();
                                        }else{
                                            finish();
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        }
                    }, 3000);


                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onStart() {
                super.onStart();
            }
        });
    }

    private void init(){
        if("0".equals(SPUtils.getStringData(this,"iss","0"))){
            Intent intent = new Intent(this, DialogActivity3.class);
            startActivityForResult(intent,2);
        }else{
            try {
                JSONObject object = new JSONObject( SPUtils.getStringData(this,"ade",""));
                if (object.getInt("code") == 0) {
                    final JSONArray array = object.getJSONObject("data").getJSONArray("list");
                    if(array.length()>0){
                        Intent intent = new Intent(SplashActivity.this, AdActivity.class);
                        startActivity(intent);
                        finish();
                    }else{
                        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }
            } catch (JSONException e) {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 2) {
            SPUtils.saveStringData(this, "iss", "1");
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            finish();
        }
    }
    @Override
    public void onBackPressed() {
        //super.onBackPressed();
    }
}
